﻿//using App.Metrics;
//using App.Metrics.Reporting.InfluxDB;
//using Microsoft.AspNetCore.Builder;
//using Microsoft.Extensions.Configuration;
//using Microsoft.Extensions.DependencyInjection;
//using System;
//using System.Collections.Generic;
//using System.Text;

//namespace SX.Web.Framework.Extensions
//{
//    public static class MetricsExtensions
//    {
//        /// <summary>
//        /// Metrics监控配置
//        /// </summary>
//        /// <param name="services"></param>
//        /// <param name="configuration"></param>
//        public static void AddMetrics(this IServiceCollection services, IConfiguration configuration)
//        {
//            #region Metrics监控配置
//            string IsOpen = configuration.GetSection("InfluxDB")["IsOpen"].ToLower();
//            if (IsOpen == "true")
//            {
//                string database = configuration.GetSection("InfluxDB")["DataBaseName"];
//                string InfluxDBConStr = configuration.GetSection("InfluxDB")["ConnectionString"];
//                string app = configuration.GetSection("InfluxDB")["app"];
//                string env = configuration.GetSection("InfluxDB")["env"];
//                string username = configuration.GetSection("InfluxDB")["username"];
//                string password = configuration.GetSection("InfluxDB")["password"];

//                var uri = new Uri(InfluxDBConStr);

//                var metrics = AppMetrics.CreateDefaultBuilder()
//                .Configuration.Configure(
//                options =>
//                {
//                    options.AddAppTag(app);
//                    options.AddEnvTag(env);
//                })
//                .Report.ToInfluxDb(
//                options =>
//                {
//                    options.InfluxDb.BaseUri = uri;
//                    options.InfluxDb.Database = database;
//                    options.InfluxDb.UserName = username;
//                    options.InfluxDb.Password = password;
//                    options.HttpPolicy.BackoffPeriod = TimeSpan.FromSeconds(30);
//                    options.HttpPolicy.FailuresBeforeBackoff = 5;
//                    options.HttpPolicy.Timeout = TimeSpan.FromSeconds(10);
//                    options.FlushInterval = TimeSpan.FromSeconds(5);
//                })
//                .Build();

//                services.AddMetrics(metrics).AddHealth();
//                services.AddMetricsReportScheduler();
//                services.AddMetricsTrackingMiddleware();
//                services.AddMetricsEndpoints();

//            }
//            #endregion
//        }

//        /// <summary>
//        /// 注入Metrics
//        /// </summary>
//        /// <param name="app"></param>
//        /// <param name="configuration"></param>
//        public static void UseMetrics(this IApplicationBuilder app, IConfiguration configuration) {
//            #region 注入Metrics
//            string IsOpen = configuration.GetSection("InfluxDB")["IsOpen"].ToLower();
//            if (IsOpen == "true")
//            {
//                app.UseMetricsAllMiddleware();
//                // Or to cherry-pick the tracking of interest
//                app.UseMetricsActiveRequestMiddleware();
//                app.UseMetricsErrorTrackingMiddleware();
//                app.UseMetricsPostAndPutSizeTrackingMiddleware();
//                app.UseMetricsRequestTrackingMiddleware();
//                app.UseMetricsOAuth2TrackingMiddleware();
//                app.UseMetricsApdexTrackingMiddleware();

//                app.UseMetricsAllEndpoints();
//                // Or to cherry-pick endpoint of interest
//                app.UseMetricsEndpoint();
//                app.UseMetricsTextEndpoint();
//                app.UseEnvInfoEndpoint();
//            }
//            #endregion
//        }
//    }
//}
